package com.controller;


import com.config.Result;
import com.pojo.Building;
import com.pojo.User;
import com.service.impl.BuildServiceImpl;
import com.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/building")
public class BuildController {

    @Autowired
    BuildServiceImpl buildService;
    @Autowired
    UserServiceImpl userService;

    /**
     * 查询全部用户
     *
     * @return
     */
    @GetMapping("/search")
    public Result<?> FindAllBuild() {
        List<Building> buildList = buildService.FindAllBuild();
        return Result.success(buildList);
    }


    /**
     * 增加公寓楼
     *
     * @param building
     * @return
     */
    @PostMapping("/addBuild")
    public Result<?> addBuild(@RequestBody Building building) {

        String name = building.getName();
        Building building2 = buildService.selectByName(name);
        System.out.println();

        if (building2 != null){
            return Result.error("-1","已存在该宿舍楼");
        }


        User user = new User();
        user.setUsername(building.getUsername().getUsername());
        User selectUser = userService.findById(user);

        if (selectUser == null || selectUser.getRole().get(0).getRoleName().equals("学生")) {
            return Result.error("-1", "没有该员工或该员工为学生");
        }
        // 添加楼
        Integer integer1 = buildService.addBuild(building);
        if (integer1 != 1){
            return Result.error("-1","添加失败");
        }
        Building building1 = buildService.selectByName(building.getName());


        // 添加宿管
        Integer integer = buildService.addBuildAdmin(building1.getId(), selectUser.getUsername());
        if (integer != 1){
            return Result.error("-1","添加失败");
        }

        return Result.success();
    }

    /**
     * 删除公寓楼
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deteleBuild/{id}")
    public Result<?> DeleteBuild(@PathVariable String id) {
        // 删除楼
        Integer integer = buildService.DeleteBuild(id);
        if (integer != 1){
            return Result.error("-1","删除失败");
        }
        // 删除楼与宿管表
        Integer integer1 = buildService.DeleteBuildEmp(id);
        if (integer1 != 1){
            return Result.error("-1","删除失败");
        }
        return Result.success();
    }


    @PutMapping("/updateBuild")
    public Result<?> updateBuild(@RequestBody Building building) {
        User user = new User();
        user.setUsername(building.getUsername().getUsername());

        User selectUser = userService.findById(user);

        if (selectUser == null || selectUser.getRole().get(0).getRoleName().equals("学生")) {
            return Result.error("-1", "没有该员工或该员工为学生");
        }
        // 更改楼名
        Integer integer = buildService.updateBuild(building);

        if (integer != 1){
            return Result.error("-1","更新失败");
        }
        // 更改楼管理员
        Integer integer1 = buildService.updateBuildEmp(building.getId(), selectUser.getUsername());
        if (integer1 != 1){
            return Result.error("-1","更新失败");
        }
        return Result.success();
    }


}
